Skip to content

Remember method/static call expression as truthy/falsey alongside @phpstan-assert-if-true argument narrowing#5880

Merged
staabm merged 28 commits into
phpstan:2.2.xfrom
phpstan-bot:create-pull-request/patch-sxihmja
Jun 17, 2026
Merged

Remember method/static call expression as truthy/falsey alongside @phpstan-assert-if-true argument narrowing#5880
staabm merged 28 commits into
phpstan:2.2.xfrom
phpstan-bot:create-pull-request/patch-sxihmja

Conversation

@phpstan-bot

@phpstan-bot phpstan-bot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

similar to #5885 but for MethodCall and StaticCall

Comment thread build/PHPStan/Build/OrChainIdenticalComparisonToInArrayRule.php
@staabm staabm force-pushed the create-pull-request/patch-sxihmja branch 2 times, most recently from ce1888f to 181e6e9 Compare June 15, 2026 15:26
@staabm staabm requested a review from VincentLanglet June 15, 2026 15:31
@staabm staabm force-pushed the create-pull-request/patch-sxihmja branch 3 times, most recently from 969b695 to 70fb90a Compare June 15, 2026 23:56
@VincentLanglet VincentLanglet removed their request for review June 16, 2026 10:32
@staabm staabm marked this pull request as draft June 16, 2026 12:10
@clxmstaab clxmstaab force-pushed the create-pull-request/patch-sxihmja branch from ff8ea80 to 24126c3 Compare June 16, 2026 12:36

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after this PR the snippet https://phpstan.org/r/9eb23e31-92a3-41e0-9f85-731ee7f3f5b2 runs endless

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan-bot and others added 16 commits June 16, 2026 21:53
…if-true` argument narrowing

- When a function/method/static call carries `@phpstan-assert*` assertions, the
  call handlers previously returned only the assert-specified argument narrowing
  and skipped `handleDefaultTruthyOrFalseyContext`, so the call expression itself
  was not remembered as truthy/falsey in the branch. Re-evaluating it (e.g.
  `if (is_readable($p)) { require $p; }`) then yielded `bool` instead of `true`.
- `FuncCallHandler`, `MethodCallHandler` and `StaticCallHandler` now union the
  asserts result with the default truthy/falsey narrowing, while preserving the
  asserts' original root expression so impossible-check detection is unaffected.
- `ImpossibleCheckTypeHelper` now ignores the self-referential narrowing entry
  (the checked call expression itself), which carries no information about
  whether the check is redundant — the informative narrowing lives in the
  argument entries.
- `RequireFileExistsRule` additionally treats `is_readable`, `is_writable`,
  `is_writeable` and `is_executable` (next to `file_exists`/`is_file`) as guards
  that prove the path exists, via a named `FILE_EXISTENCE_FUNCTIONS` constant.
- Fixed a genuine `X && !X` bug in the build-only
  `OrChainIdenticalComparisonToInArrayRule::getSubjectAndValue()` that the
  improved narrowing newly detects.
@staabm staabm force-pushed the create-pull-request/patch-sxihmja branch from 9c4e11c to 77b8552 Compare June 16, 2026 19:53
@staabm staabm marked this pull request as ready for review June 16, 2026 20:01
@staabm staabm requested a review from VincentLanglet June 16, 2026 20:01
@phpstan-bot

Copy link
Copy Markdown
Collaborator Author

This pull request has been marked as ready for review.

@staabm staabm changed the title Remember call expression as truthy/falsey alongside @phpstan-assert-if-true argument narrowing Remember method/static call expression as truthy/falsey alongside @phpstan-assert-if-true argument narrowing Jun 16, 2026
@staabm staabm merged commit ca1918c into phpstan:2.2.x Jun 17, 2026
672 of 674 checks passed
@staabm staabm deleted the create-pull-request/patch-sxihmja branch June 17, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants